package com.cloudbroker.bcs.platform.dav.middle.bizservice.impl;

import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.model.Pagination;
import com.cloudbroker.bcs.common.model.SortMarker;
import com.cloudbroker.bcs.common.util.DateTimeUtil;
import com.cloudbroker.bcs.common.util.JSONUtil;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.common.util.ValueUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes.FavoriteErr;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSFields;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.favorite.FavoriteBizService;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.favorite.result.FavoriteDetailResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.favorite.result.FavoriteListData;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.favorite.result.FavoritePageResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.favorite.vo.FavoriteAddForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.favorite.vo.FavoriteListForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.favorite.vo.FavoriteUpdateForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.favorite.vo.FavoriteViewForm;
import com.cloudbroker.bcs.platform.dav.common.constant.SysContants;
import com.cloudbroker.bcs.platform.dav.middle.context.HSBCPSBizDataContext;
import com.cloudbroker.bcs.platform.dav.middle.entity.Favorite;
import com.cloudbroker.bcs.platform.dav.middle.service.FavoriteService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FavoriteBizServiceImpl implements FavoriteBizService {
    
    @Resource
    private FavoriteService favoriteService;
    
    @Override
    public FavoritePageResult funcFavoriteList(FavoriteListForm param) {

        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        String favorite_type = param.getFavorite_type();
        String position_str = param.getPosition_str();
        Integer request_num = SysContants.DEFAULT_REQUSET_NUM;
        if (param.getRequest_num() != null) {
            request_num = Integer.parseInt(param.getRequest_num());
        }
        Integer page_no = SysContants.MIN_REQUEST_NUM;
        if (param.getPage_no() != null) {
            page_no = Integer.parseInt(param.getPage_no());
        }
        Favorite favorite = new Favorite();
        
        Pagination page = new Pagination();
        page.setPage_size(request_num);
        page.setPage_no(page_no);
        
        favorite.setBroker_id(broker_id);
        favorite.setFavorite_type(favorite_type);
        favorite.setPosition_str(position_str);
        favorite.setTenant_id(HSBCPSBizDataContext.getTenantId());
        SortMarker sm = new SortMarker();
        sm.setField(HSBCPSFields.POSITION_STR);
        sm.setAsc(false);
        List<Favorite> favorites = this.favoriteService.queryWithPagination(favorite, page, sm);

        FavoritePageResult favoritePageResult = new FavoritePageResult();
        List<FavoriteListData> favoriteListDataList = new ArrayList<>();
        for(Favorite favoriteVo:favorites)
        {
            FavoriteListData favoriteView = new FavoriteListData();
            favoriteView.setFavorite_id(favoriteVo.getFavorite_id());
            favoriteView.setFavorite_time(DateTimeUtil.toString(favoriteVo.getCreate_time(),DateTimeUtil.FORMAT_YYYYMMDDHHMMSS));
            favoriteView.setFavorite_type(favoriteVo.getFavorite_type());
            favoriteView.setFavorite_title(favoriteVo.getFavorite_title());
            favoriteView.setFavorite_content(favoriteVo.getFavorite_content());
            /*新加收藏概要*/
            favoriteView.setFavorite_abstract(favoriteVo.getFavorite_abstract());
            favoriteView.setBroker_id(favoriteVo.getBroker_id());
            favoriteView.setFavorite_source(favoriteVo.getFavorite_source());
            favoriteView.setPosition_str(favoriteVo.getPosition_str());
            favoriteListDataList.add(favoriteView);
        }
        favoritePageResult.setPage_no(page.getPage_no());
        favoritePageResult.setPage_size(page.getPage_size());
        favoritePageResult.setTotal_item_num(page.getTotal_item_num());
        favoritePageResult.setData_list(favoriteListDataList);

       return VOUtil.setSuccessResult(favoritePageResult);
    }
    
    @Override
    public FavoriteDetailResult funcFavoriteDetail(FavoriteViewForm param) {
        FavoriteDetailResult favoriteDetailResult = new FavoriteDetailResult();
        String favorite_id = param.getFavorite_id();
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        
        Favorite favorite = this.favoriteService.get(favorite_id);
        
        if (favorite == null || !broker_id.equals(favorite.getBroker_id())) {
            return VOUtil.genErrorResult(FavoriteErr.FAVORITE_NOT_EXIST,FavoriteDetailResult.class);
        }

        favoriteDetailResult.setFavorite_id(favorite.getFavorite_id());
        favoriteDetailResult.setFavorite_time(favorite.getCreate_time());
        favoriteDetailResult.setFavorite_type(favorite.getFavorite_type());
        favoriteDetailResult.setFavorite_title(favorite.getFavorite_title());
        /*新加收藏概要*/
        favoriteDetailResult.setFavorite_abstract(favorite.getFavorite_abstract());
        favoriteDetailResult.setFavorite_content(favorite.getFavorite_content());
        favoriteDetailResult.setFavorite_source(favorite.getFavorite_source());

        return VOUtil.setSuccessResult(favoriteDetailResult);
    }
    
    @Override
    public HSBCPSBackVO funcFavoriteDel(FavoriteViewForm param) {
        String favorite_id = param.getFavorite_id();

        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        
        Favorite favorite = this.favoriteService.get(favorite_id);
        
        // 判断收藏是否存在
        if (favorite == null || !broker_id.equals(favorite.getBroker_id())) {
            return VOUtil.genErrorResult(FavoriteErr.FAVORITE_NOT_EXIST,HSBCPSBackVO.class);
        }
        this.favoriteService.delete(favorite_id);
        return VOUtil.genSuccessResultVo();
    }
    
    @Override
    public FavoriteDetailResult funcFavoriteSave(FavoriteAddForm param) {
        FavoriteDetailResult favoriteDetailResult = new FavoriteDetailResult();
        String favorite_type = param.getFavorite_type();
        String favorite_title = param.getFavorite_title();
        String favorite_content = param.getFavorite_content();
        Integer favorite_source = ValueUtil.getInt(param.getFavorite_source());
        /*新加收藏概要*/
        String favorite_abstract = param.getFavorite_abstract();
        favorite_content = JSONUtil.bodyNotEmpty(favorite_content);

        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        
        if ("1".equals(favorite_type)) {
            //查询新闻是否已收藏
            Favorite example = new Favorite();
            example.setFavorite_title(favorite_title);
            example.setFavorite_type(favorite_type);
            example.setBroker_id(broker_id);
            List<Favorite> favoriteTemp = favoriteService.query(example);
            if (favoriteTemp != null && !favoriteTemp.isEmpty()) {
                return VOUtil.genErrorResult(FavoriteErr.NEWS_HAS_FAVORITE,FavoriteDetailResult.class);
            }
        }
        Date now = new Date();
        Favorite favorite = new Favorite();
        favorite.setBroker_id(broker_id);
        favorite.setCreate_time(now);
        favorite.setFavorite_source(favorite_source);
        /*favorite.setFavorite_time(now);*///zjpopo
        favorite.setPosition_str(DateTimeUtil.getCurrentDateString(DateTimeUtil.FORMAT_YYYYMMDDHHMMSSSSS_NO_BREAK));
        favorite.setFavorite_content(ValueUtil.getString(favorite_content).replace("&lt;", "<").replace("&gt;", ">"));
        favorite.setFavorite_title(ValueUtil.getString(favorite_title).replace("&lt;", "<").replace("&gt;", ">"));
        /*新加收藏概要*/
        favorite.setFavorite_abstract(ValueUtil.getString(favorite_abstract).replace("&lt;", "<").replace("&gt;", ">"));
        favorite.setFavorite_type(favorite_type);
        favorite.setFavorite_source(favorite_source);
        this.favoriteService.save(favorite);
        
        Favorite favorite2 = this.favoriteService.getByPositionStr(favorite.getPosition_str());
        favoriteDetailResult.setFavorite_id(favorite2.getFavorite_id());
        
        return VOUtil.setSuccessResult(favoriteDetailResult);
    }
    
    @Override
    public HSBCPSBackVO funcFavoriteUpdate(FavoriteUpdateForm param) {
        Favorite favorite = favoriteService.get(String.valueOf(param.getFavorite_id()));
        
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        
        if (favorite == null || !broker_id.equals(favorite.getBroker_id())) {
            return VOUtil.genErrorResultVo(FavoriteErr.FAVORITE_NOT_EXIST);
        }
        favorite.setFavorite_type(String.valueOf(param.getFavorite_type()));
        favorite.setFavorite_title(param.getFavorite_title());
        favorite.setFavorite_content(param.getFavorite_content());
        /*新加收藏概要*/
        favorite.setFavorite_abstract(param.getFavorite_abstract());
        favoriteService.update(favorite);
        return VOUtil.genSuccessResultVo();
    }
}
